package com.laikan.reader.core.utils;

import android.content.Context;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.View;
import android.view.WindowManager;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.ListView;
import com.laikan.reader.R;

/**
 * 测量工具类
 *
 * @author lmine
 */
public class MeasureUtil {

    public final static int PX = TypedValue.COMPLEX_UNIT_PX;
    public final static int DIP = TypedValue.COMPLEX_UNIT_DIP;
    public final static int SP = TypedValue.COMPLEX_UNIT_SP;

    public static int width;// 屏幕宽
    public static int height;// 屏幕高

    /**
     * @param unit 单位 </br>0 px</br>1 dip</br>2 sp
     * @param value size 大小
     * @param context
     *
     * @return
     */
    public static float getDimensionPixelSize(int unit, float value, Context context) {
        DisplayMetrics metrics = new DisplayMetrics();
        WindowManager wm = (WindowManager)context.getSystemService(Context.WINDOW_SERVICE);
        wm.getDefaultDisplay().getMetrics(metrics);
        switch (unit) {
            case PX:
                return value;
            case DIP:
            case SP:
                return TypedValue.applyDimension(unit, value, metrics);
            default:
                throw new IllegalArgumentException("unknow unix");
        }
    }

    /**
     * 测量listview的高度
     *
     * @param listview
     */
    public static void setListViewHeight(ListView listview) {

        ListAdapter listAdapter = listview.getAdapter();
        if (listAdapter == null) {
            return;
        }
        int totalHeight = 0;
        for (int i = 0; i < listAdapter.getCount(); i++) {
            View listItem = listAdapter.getView(i, null, listview);
            if (listItem == null) {
                continue;
            }
            // 对每一个item进行测量
            listItem.measure(0, 0);
            totalHeight += (listItem.getMeasuredHeight() + listview.getDividerHeight());
            System.out.println(totalHeight + "---------------------------------");
        }
        listview.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, totalHeight));

    }

    /**
     * 获取屏幕分辨率
     *
     * @param context
     */
    public static void getScreenResolution(Context context) {
        DisplayMetrics dm = new DisplayMetrics();
        WindowManager wm = (WindowManager)context.getSystemService(Context.WINDOW_SERVICE);
        wm.getDefaultDisplay().getMetrics(dm);

        width = dm.widthPixels;
        height = dm.heightPixels;
    }

    /**
     * TODO 此方法有问题，查明使用后决定是否删除
     *
     * @return
     */
    public static int getScreenWidth() {
        return width;
    }

    /**
     * TODO 此方法有问题，查明使用后决定是否删除
     *
     * @return
     */
    public static int getScreenHeight() {
        return height;
    }

    public static int dip2px(Context context, float dipValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int)(dipValue * scale + 0.5f);
    }

    public static int px2dip(Context context, float pxValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int)(pxValue / scale + 0.5f);
    }

    /**
     * 将px值转换为dip或dp值，保证尺寸大小不变
     *
     * @param pxValue
     * @param scale （DisplayMetrics类中属性density）
     *
     * @return
     */
    public static int px2dip(float pxValue, float scale) {
        return (int)(pxValue / scale + 0.5f);
    }

    /**
     * 将dip或dp值转换为px值，保证尺寸大小不变
     *
     * @param dipValue
     * @param scale （DisplayMetrics类中属性density）
     *
     * @return
     */
    public static int dip2px(float dipValue, float scale) {
        return (int)(dipValue * scale + 0.5f);
    }

    /**
     * 将px值转换为sp值，保证文字大小不变
     *
     * @param pxValue
     * @param fontScale （DisplayMetrics类中属性scaledDensity）
     *
     * @return
     */
    public static int px2sp(float pxValue, float fontScale) {
        return (int)(pxValue / fontScale + 0.5f);
    }

    /**
     * 将sp值转换为px值，保证文字大小不变
     *
     * @param spValue
     * @param fontScale （DisplayMetrics类中属性scaledDensity）
     *
     * @return
     */
    public static int sp2px(float spValue, float fontScale) {
        return (int)(spValue * fontScale + 0.5f);
    }

    /**
     * 测量gridview的高度
     *
     * @param context
     * @param gridView
     * @param numColumns
     */
    public static void setGridViewHeight(Context context, GridView gridView, int numColumns) {

        ListAdapter listAdapter = gridView.getAdapter();
        if (listAdapter == null) {
            return;
        }
        if (numColumns == 0) {
            System.out.println("除数不能为0");
            return;
        }
        int totalHeight = 0;
        int dimension = (int)context.getResources().getDimension(R.dimen.icome_gv_horspace) + 10;
        int count = listAdapter.getCount();
        if (count % 4 == 0) {
            count = count - 1;
        }
        for (int i = 0; i < listAdapter.getCount() / numColumns + 1; i++) {
            View listItem = listAdapter.getView(i, null, gridView);
            if (listItem == null) {
                continue;
            }
            // 对每一个item进行测量
            listItem.measure(0, 0);
            totalHeight += listItem.getMeasuredHeight();
            totalHeight += dimension;

        }

        gridView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, totalHeight));
    }
}
